package design_6_adapter.demo1.handler.impl;

import design_6_adapter.demo1.handler.HandlerAdapter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * 静态资源 适配器
 *
 * @author wangsong
 * @email 1720696548@qq.com
 * @date 2022/4/13 9:57
 */
public class HttpRequestHandlerAdapter implements HandlerAdapter {

	/**
	 * 静态文件根路径
	 */
	private final static String STATIC_PATH = "src/design_6_adapter/demo1/controller/static1";


	@Override
	public boolean supports(String uri) {
		// 判断文件是否存在
		File file = new File(STATIC_PATH + uri);
		return file.exists();
	}

	@Override
	public Object handle(String uri) {
		// 静态文件根路径
		File file = new File(STATIC_PATH + uri);
		//outputStream 是一个抽象类， 需要子类进行实例化，一位置只能进行文件处理
		OutputStream output = null;
		try {
			output = new FileOutputStream(file);
			//关闭输出
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return output;
	}
}
